# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Maintainer: Maxim Baz <$pkgname at maximbaz dot com>
# Maintainer: Daniel M. Capella <polyzen@archlinux.org>

pkgname=bash-language-server
pkgver=4.9.1
pkgrel=1
pkgdesc="Bash language server implementation based on Tree Sitter and its grammar for Bash"
arch=("any")
url="https://github.com/bash-lsp/bash-language-server"
license=("MIT")
depends=("nodejs")
makedepends=("git" "jq" "typescript" "yarn")
optdepends=('bash-completion: for CLI args completion'
            'man-db: for documentation via manual pages'
            'shellcheck: to enable linting')
source=("$pkgname-$pkgver.tar.gz::$url/archive/server-$pkgver.tar.gz")
b2sums=('70562dc235c27698bfc486a59e7eec84c86bb3e38788c3ae2e80874209279636aeb765947a044aeaf813de895fc574114995b739bbb2760998e01e86aa54a8e5')

build() {
    cd "$pkgname-server-$pkgver"
    yarn --frozen-lockfile
    yarn run compile
}

package() {
    cd "$pkgname-server-$pkgver/server"

    # Emulate `npm prune --production`
    cp package.json{,.bak}
    read -ra devDependencies < <(jq -r '.devDependencies | keys | join(" ")' package.json)
    yarn remove --frozen-lockfile "${devDependencies[@]}"
    mv package.json{.bak,}

    install -d "$pkgdir/usr/lib/$pkgname"
    cd "$pkgdir/usr/lib/$pkgname"
    cp -a "$srcdir/$pkgname-server-$pkgver/server/"* .
    chmod +x out/cli.js
    install -Dm644 -t "$pkgdir/usr/share/licenses/$pkgname/" "$srcdir/$pkgname-server-$pkgver/LICENSE"

    install -d "$pkgdir/usr/bin"
    ln -s "/usr/lib/$pkgname/out/cli.js" "$pkgdir/usr/bin/$pkgname"
}
